package com.mifengs.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.BannerBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.loader.GlideImageLoader;
import com.mifengs.mall.ui.web.CommonWebActivity;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.ToIntentUtil;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by lihaoqi on 2017/10/31.
 */

public class HomeFiveFloorTypeAdapter extends  DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ProductQuickAdapter mProductQuickAdapter;
    private FloorBean floorBean;
    private int type =1; // 1表示旧楼层 2表示新楼层
    public HomeFiveFloorTypeAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void setFloorData(FloorBean floorBean ,int type){
        this.floorBean = floorBean;
        this.type = type;
        notifyDataSetChanged();
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return  new LinearLayoutHelper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FiveFloorViewHolder(LayoutInflater.from(mContext).inflate(R.layout.fragment_five_floor,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof FiveFloorViewHolder){
            FiveFloorViewHolder viewHolder = (FiveFloorViewHolder)holder;
            final RecyclerView mRecyclerView = viewHolder.mRecyclerView;
            ImageView five_floor_one_img = viewHolder.five_floor_one_img;
            ImageView five_floor_two_img = viewHolder.five_floor_two_img;
            ImageView five_floor_three_img =viewHolder.five_floor_three_img;
            TextView five_floor_type_name = viewHolder.five_floor_type_name;
            Banner banner = viewHolder.banner;
            if(type == 1){
                viewHolder.llayout.setVisibility(View.VISIBLE);
            }else{
                viewHolder.llayout.setVisibility(View.GONE);
            }
            if(floorBean !=null){
                if(floorBean.getBanners().size()>0){
                    banner.setVisibility(View.VISIBLE);
                    List<BannerBean> banners = floorBean.getBanners();
                    banner.setImages(banners)
                            .setImageLoader(new GlideImageLoader())
                            .setDelayTime(5000)
                            .setOnBannerListener(new OnBannerListener() {
                                @Override
                                public void OnBannerClick(int position) {
                                    ToIntentUtil.toCommonIntent(floorBean.getBanners().get(position).getActionUrl(),mContext);
                                }
                            })
                            .start();
                }else{
                    banner.setVisibility(View.GONE);
                }
                five_floor_type_name.setText("--------  "+floorBean.getText()+"  --------");
                for(int i = 0;i<floorBean.getAdverts().size();i++ ){
                    AdvertBean adverts = floorBean.getAdverts().get(i);
                    switch (i){
                        case 0:
                            ImageLoadUtil.displayImage(mContext,adverts.getImg(),five_floor_one_img);
                            break;
                        case 1:
                            ImageLoadUtil.displayImage(mContext,adverts.getImg(),five_floor_two_img);
                            break;
                        case 2:
                            ImageLoadUtil.displayImage(mContext,adverts.getImg(),five_floor_three_img);
                            break;
                    }
                }

                five_floor_one_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdvertBean adverts = floorBean.getAdverts().get(0);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
                five_floor_two_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdvertBean adverts = floorBean.getAdverts().get(1);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
                five_floor_three_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdvertBean adverts = floorBean.getAdverts().get(2);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });

                final List<AdvertBean> tempAdvertBeanList = floorBean.getAdverts();
                final List<AdvertBean> advertBeanList = new ArrayList<AdvertBean>();
                for(int i = 3;i<tempAdvertBeanList.size();i++){
                    AdvertBean advertBean = tempAdvertBeanList.get(i);
                    advertBeanList.add(advertBean);
                }
//            mRecyclerView.addItemDecoration(new DividerGridItemDecoration(mContext));
                mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,2));
                mRecyclerView.setNestedScrollingEnabled(false);
                if(type == 1){
                    mProductQuickAdapter= new ProductQuickAdapter(R.layout.home_item_product_info,advertBeanList,mContext);
                }else{
                    mProductQuickAdapter= new ProductQuickAdapter(R.layout.home_item_product_info,tempAdvertBeanList,mContext);
                }

                mRecyclerView.setAdapter(mProductQuickAdapter);
                mProductQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AdvertBean adverts;
                        if(type == 1){
                             adverts = advertBeanList.get(position);
                        }else{
                             adverts = tempAdvertBeanList.get(position);
                        }

                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
            }

        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }


    public class FiveFloorViewHolder extends RecyclerView.ViewHolder{

        Banner banner;
        RecyclerView mRecyclerView;
        ImageView five_floor_one_img;
        ImageView five_floor_two_img;
        ImageView five_floor_three_img;
        TextView  five_floor_type_name;
        LinearLayout llayout;
        public FiveFloorViewHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);
            mRecyclerView = (RecyclerView)itemView.findViewById(R.id.grid_list);
            five_floor_one_img = (ImageView)itemView.findViewById(R.id.five_floor_one_img);
            five_floor_two_img = (ImageView)itemView.findViewById(R.id.five_floor_two_img);
            five_floor_three_img = (ImageView)itemView.findViewById(R.id.five_floor_three_img);
            five_floor_type_name = (TextView)itemView.findViewById(R.id.five_floor_type_name);
            llayout =  (LinearLayout)itemView.findViewById(R.id.llayout);

        }
    }
}
